package com.meida.student.app.user.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.PageInterceptor;
import com.meida.common.mybatis.model.PageParams;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.security.OpenHelper;
import com.meida.common.utils.DateUtils;
import com.meida.module.bbs.client.entity.BbsComment;
import com.meida.module.bbs.client.entity.BbsContent;
import com.meida.module.bbs.provider.service.BbsCommentService;
import com.meida.module.bbs.provider.service.BbsContentService;
import com.meida.module.bus.provider.utils.MyDateUtils;
import com.meida.module.system.client.entity.SysCollecon;
import com.meida.module.user.client.entity.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 赞和收藏列表
 */
@Component
public class SysColleconPageListHandler implements PageInterceptor<SysCollecon> {

    @Autowired
    private BbsContentService bbsContentService;

    @Autowired
    private BbsCommentService bbsCommentService;

    @Autowired
    private HttpServletRequest httpServletRequest;


    @Override
    public void prepare(CriteriaQuery<SysCollecon> cq, PageParams pageParams, EntityMap params) {
        Long userId = OpenHelper.getUserId();
        cq.select(AppUser.class, "nickName", "avatar");
        cq.createJoin(AppUser.class).setMainField("userId").setJoinField("userId");
        cq.ne("optType", 3);
        cq.eq("targetUserId", userId);
    }

    @Override
    public void complete(CriteriaQuery<SysCollecon> cq, List<EntityMap> result, EntityMap extra) {
        String targetTimezone = httpServletRequest.getHeader("targetTimezone");
        result.forEach(entityMap -> {
            Date createTime = entityMap.get("createTime");
            String showTime = DateUtils.formatFromTodayByTargetTimezone(createTime,targetTimezone);
            entityMap.put("showTime", showTime);
            setShowContent(entityMap);
        });
    }

    void setShowContent(EntityMap entityMap) {
        entityMap.put("showContent", "");
        String targetEntity = entityMap.get("targetEntity");
        Long targetId = entityMap.getLong("targetId");
        if (BbsContent.class.getSimpleName().equals(targetEntity)) {
            //收藏点赞都有帖子
            //帖子的话
            BbsContent bbsContent = bbsContentService.getById(targetId);
            if (FlymeUtils.isNotEmpty(bbsContent)) {
                entityMap.put("showContent", bbsContent.getTitle());
                entityMap.put("coverImage", bbsContent.getCoverImage());
                entityMap.put("contentType", bbsContent.getContentType());
            }
        }
        if (BbsComment.class.getSimpleName().equals(targetEntity)) {
            BbsComment bbsComment = bbsCommentService.getById(targetId);
            if (FlymeUtils.isNotEmpty(bbsComment)) {
                entityMap.put("showContent", bbsComment.getReplyContent());
                entityMap.put("coverImage", "");
                entityMap.put("contentType", "");
            }
        }

    }
}
